import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_class2/3681/accountRelated/ResetPWPage2.dart';
import 'package:flutter_class2/common/global.dart';
import 'package:toast/toast.dart';

import '../MainPage.dart';
import 'account_controller.dart';

class ResetPWPage extends StatelessWidget {
  Widget build(BuildContext context){
    return ResetPWPageState();
  }
}

class ResetPWPageState extends StatefulWidget {
  @override
  _ResetPWPageState createState() => new _ResetPWPageState();
}

class _ResetPWPageState extends State<ResetPWPageState> {

  String idStr = '';//先初始化一个值，避免后面出错

  @override

  Widget build(BuildContext context) {

    Global global = new Global();

    var accountDatabaseHelper = new AccountDatabaseHelper();

    final logo = Hero(// 创建hero动画，沟通两个组件之间的tag必须统一
      tag: 'headPic',
      child: CircleAvatar(
        backgroundColor: Colors.transparent,
        radius: 48.0,
        child: ClipRRect(
          borderRadius: BorderRadius.all(Radius.circular(90)),
          child: Image.asset('images/meng.png'),
        ),
      ),
    );

    final id = TextField(
      autofocus: false,
      maxLines: 1,
      keyboardType: TextInputType.url,
      inputFormatters: [
        LengthLimitingTextInputFormatter(12),
        WhitelistingTextInputFormatter(RegExp('[a-zA-Z0-9]')),//限制输入内容为规定格式
      ],
      decoration: InputDecoration(
        hintText: '请输入要重置的用户名',
        contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
        border: global.diyColorTextFieldBorder(Colors.black54),// 输入框的颜色
        enabledBorder: global.diyColorTextFieldBorder(Colors.black54),// 输入框可编辑（但未选中）时的颜色
        focusedBorder: global.diyColorTextFieldBorder(Colors.black54),// 输入框获取焦点时的颜色
      ),
      onChanged: (value){
        idStr = value;
      },
    );

    final loginButton = Hero(
        tag: 'loginButton',
        child: Padding(
          padding: EdgeInsets.symmetric(vertical: 16.0),
          child: RaisedButton(
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(16),
            ),
            padding: EdgeInsets.all(12),
            color: Colors.black54,
            child: Text('重置密码', style: TextStyle(color: Colors.white)),
            onPressed: () async {
              Account account = await accountDatabaseHelper.getAccount(idStr);
              if(account != null ){
                  String questionStr = account.resetPWQuestion;
                  String answerStr = account.resetPWAnswer;
                  Navigator.push(context, MaterialPageRoute(builder: (context)=>ResetPWPage2(idStr: idStr,questionStr: questionStr,answerStr: answerStr,)));
              }else{
                  Toast.show('用户名有误', context);
              }
            },
          ),
        )
    );

    return Scaffold(
      backgroundColor: Colors.white,
      body: Center(
        child: Container(
          padding: EdgeInsets.only(left: 24.0, right: 24.0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              logo,
              SizedBox(height: 48.0),
              id,
              SizedBox(height: 16.0),
              loginButton,
            ],
          ),
        )
      ),
    );
  }
}